package com.kiln.juxingqs.fragment

import android.content.Intent
import android.graphics.Color
import androidx.lifecycle.Observer
import client.global.commlib.base.BaseFragment
import client.global.commlib.dialog.CameraOrSelectDialog
import client.global.commlib.toast.toast
import client.global.commlib.utils.Util
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.event.EventUserInfo
import com.kiln.juxingqs.databinding.FragmentTabfourBinding
import com.kiln.juxingqs.dialog.MsgTitleDialog
import com.kiln.juxingqs.ui.*
import com.kiln.juxingqs.ui.vm.MainViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class TabFourFragment : BaseFragment<FragmentTabfourBinding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_tabfour
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    override fun initWidget() {
        EventBus.getDefault().register(this)

        binding.event = this
        binding.refreshLayout.setOnRefreshListener {
            viewModel.userInfo()
        }
//        Util.setBg(Color.TRANSPARENT, requireActivity().window)

        viewModel.callUser.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    binding.userInfo = it.data

                    mImgLoader.load(it.data!!.avatar).error(R.mipmap.ic_launcher_round)
                        .into(binding.userAvatar)

//                    binding.realnameOrUserinfo = when (it.data?.status) {
//                        0 -> "实名认证 >"
//                        else -> "个人信息 >"
//                    }
//判断是否填写有效期后，跳转填写
                    if (it.data!!.dg_status == 1)
                        startActivity(Intent(requireActivity(), RealAbleDateVerifyActivity::class.java))

                    var realName: String = ""
                    when (it.data?.status) {
                        0 -> realName = "实名认证 >"
                        1 -> realName = "个人信息 >"
                        2 -> realName = "个人信息 >"
                    }
                    binding.realnameOrUserinfo = realName


                }
                binding.refreshLayout.isRefreshing = false
            }
        })

        viewModel.rechargeOrder.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    it.data?.let {
                        if (it.recharge_list != null && it.recharge_list.size > 0) {

//                            viewModel.getRechargeOrderList()
                            startActivity(Intent(context, RechargeOrderActivity::class.java))
                        } else {
                            startActivity(Intent(context, CreateRechargeActivity::class.java))
                        }
                    }
                    viewModel.rechargeOrder.value = null
                }

            }
        })
    }

    //用户信息修改成功时，回调刷新
    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun updateUserInfo(event: EventUserInfo?) {
        viewModel.userInfo()
    }

    override fun onResume() {
        super.onResume()
        viewModel.userInfo()
    }

    fun imgSelect() {
        MsgTitleDialog(requireContext()).apply {
            okTxt = "去设置"
            cancleTxt = "取消"
            title = "设置用户头像？"
            ok = {
                CameraOrSelectDialog(requireActivity()).apply {
                    suc = {
                        viewModel.userAvatar.value = it
                        viewModel.setAvatar(requireActivity())
                    }
                }.show()
            }
        }.show()
    }


    fun tx() {
        startActivity(Intent(requireContext(), TxActivity::class.java))
    }

    fun recharge() {
        viewModel.getRechargeOrder()
    }

    fun goMyBank() {
        startActivity(Intent(context, BankManagerActivity::class.java))
    }

    //资金明细
    fun availableAssets() {
        startActivity(Intent(context, AvailableAssetsActivity::class.java))
    }

    //冻结明细
    fun fixedAssets() {
        toast("暂未开放")
//        startActivity(Intent(context, FixedAssetsActivity::class.java))
    }

    fun xyzr() {
        startActivity(Intent(context, ZrXyActivity::class.java))
    }

    fun djzr() {
        toast("暂未开放")
//        startActivity(Intent(context, ZrDjActivity::class.java))
    }

    //商品盈亏
    fun ppProfitAndLoss() {
        toast("暂未开放")
//        startActivity(Intent(context, PpProfitAndLossActivity::class.java))
    }

    fun setaddr() {
        startActivity(Intent(context, AddrMangerActivity::class.java))
    }

    fun walletDetail() {
        startActivity(Intent(context, QbyeActivity::class.java))
    }


    fun tuiguang() {
        startActivity(Intent(context, TuiGuangActivity::class.java))
    }

    fun coupon() {
        startActivity(Intent(context, MeCouponActivity::class.java))
    }

    fun couponList() {
        startActivity(Intent(context, AllCouponListActivity::class.java))
    }

    fun moRen() {
        toast("暂未开放")
    }

    //切换到平台资讯
    fun goPtzx() {
        viewModel.switchValue.value = 2
    }

    fun wdtj() {
        startActivity(Intent(context, WdtjActivity::class.java))
    }

    fun goJy() {
        viewModel.switchValue.value = 1
    }

    /**
     * 0 未实名
     * 1 审核中
     * 2 已审核
     */
    fun personInfo(status: Int) { // 判断是否实名认证 个人信息
        if (status == 0) {
            startActivity(Intent(context, RealNameVerifyActivity::class.java))
        } else {
            startActivity(Intent(context, PersonInfoActivity::class.java))
        }
    }

    fun gpjjF() { // 挂牌居间费明细
        startActivity(Intent(context, GpjjFActivity::class.java))
    }

    fun wdhy() { // 我的会员
//        toast("暂未开放")
        startActivity(Intent(context, WdhyActivity::class.java))
    }

    //我的竞拍
    fun wdjp() { // 我的竞拍
        startActivity(Intent(context, WdjpActivity::class.java))
    }

    //我的商品wa
    fun wdpp() { // 我的商品
        startActivity(Intent(context, PpProfitAndLossActivity::class.java))
    }

    fun wdgz() { // w
        startActivity(Intent(context, WdgzActivity::class.java))
    }

    // 点对点交易
    fun dddjy() { // w
        startActivity(Intent(context, DddjyActivity::class.java))
    }

    //设置
    fun setting() {
        startActivity(Intent(context, SettingActivity::class.java))
    }

    //我的提货
    fun wdth() {
        startActivity(Intent(context, WdthActivity::class.java))
    }

    //收货地址
    fun shouhuodz() {
        startActivity(Intent(context, AddrMangerActivity::class.java))
    }

    fun bljmx() { // 保留金明细
        startActivity(Intent(context, BljmxActivity::class.java))
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}